//
//  DownloadManager.h
//  DownloadManagerDemo
//
//  Created by ejiang on 2021/12/9.
//

#import <Foundation/Foundation.h>
#import "DownloadTask.h"

NS_ASSUME_NONNULL_BEGIN

@protocol DownloadManagerDelegate <NSObject>

- (void)downloadManagerDidChangeProgress:(DownloadTask *)task;

- (void)downloadManagerDidFinished:(DownloadTask *)task error:(NSError *__nullable)error;

- (void)downloadManagerDidPaused:(DownloadTask * __nullable)task;

- (void)downloadManagerDidResume:(DownloadTask * __nullable)task;

- (void)downloadManagerDidDeleteTask:(DownloadTask *)task;

- (void)downloadManagerDidRetryTask:(DownloadTask *)task;

@end

@interface DownloadManager : NSObject

/// 单例
+ (instancetype)sharedDownload;

/// 最大同时下载数, 默认为 1, 最大 5，最小 1
@property (nonatomic, assign) NSUInteger maxDownloadNum;

@property (nonatomic, weak) id <DownloadManagerDelegate> delegate;

@property (nonatomic, copy) void(^backgroundCompletionHandler)(void);

/// 添加下载任务
/// @param downloadPath 下载路径
/// @param targetName 下载文件名称
/// @param targetDir 下载目标文件夹，Document下的相对文件夹路径
- (void)addDownload:(NSString *)downloadPath targetName:(NSString *__nullable)targetName targetDir:(NSString *__nullable)targetDir;

- (void)addDownloadTask:(DownloadTask *)task;

/// 所有下载任务
- (NSMutableArray <DownloadTask *>*)tasks;

/// 开始全部任务
- (void)startAll;

/// 暂停全部任务
- (void)pauseAll;
- (void)pause:(DownloadTask *)task;

/// 恢复全部任务
- (void)resumeAll;
- (void)resume:(DownloadTask *)task;

- (void)deleteDownloadTask:(NSString *)taskId;

/// 当前对应下载链接是否存在下载任务，不考虑任务状态
/// @param downloadPath 下载链接
- (BOOL)isExistDownloadTask:(NSString *)downloadPath;

/// 查找上传任务，不考虑任务状态
/// @param downloadPath 下载链接
- (DownloadTask *__nullable)searchDownloadTask:(NSString *)downloadPath;

- (void)retryDownloadTask:(DownloadTask *)task;

@end

NS_ASSUME_NONNULL_END
